<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="宿舍id" prop="dormId">
        <!-- <el-input v-model="dataForm.dormId" placeholder="宿舍id"></el-input> -->
        <el-cascader
          :props="defaultProps"
          v-model="dormValue"
          :options="menus"
          @change="handleChange"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="报修事项" prop="reason">
        <el-input v-model="dataForm.reason" placeholder="报修事项"></el-input>
      </el-form-item>
      <el-form-item label="证明照片" prop="img">
        <!-- <el-input v-model="dataForm.img" placeholder="证明照片"></el-input> -->
        <single-upload v-model="dataForm.img"></single-upload>
        <!-- 1234 -->
      </el-form-item>
      <!-- <el-form-item label="状态" prop="status">
        <el-input
          v-model="dataForm.status"
          placeholder="状态（0：已发布 1：维修员确认，等待维修 2：维修员已完成）"
        ></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="维修工" prop="fixId">
        <el-select v-model="value" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="修好照片" prop="confirmImg"> -->
        <!-- <el-input v-model="dataForm.confirmImg" placeholder="修好照片"></el-input> -->
        <!-- <single-upload v-model="dataForm.confirmImg"></single-upload>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload";
export default {
  components: { SingleUpload },
  data() {
    return {
      flag: 0,
      defaultProps: {
        children: "children",
        label: "name",
        value: "id",
      },
      dormValue: "",
      menus: [],
      visible: false,
      dataForm: {
        id: 0,
        dormId: "",
        reason: "",
        img: "",
        status: "",
        fixId: "",
        confirmImg: "",
      },
      dataRule: {
        // dormId: [
        //   { required: true, message: "宿舍id不能为空", trigger: "blur" },
        // ],
        // reason: [
        //   { required: true, message: "报修事项不能为空", trigger: "blur" },
        // ],
        // img: [{ required: true, message: "证明照片不能为空", trigger: "blur" }],
        // status: [
        //   {
        //     required: true,
        //     message:
        //       "状态（0：已发布 1：维修员确认，等待维修 2：维修员已完成）不能为空",
        //     trigger: "blur",
        //   },
        // ],
        // fixId: [{ required: true, message: "维修id不能为空", trigger: "blur" }],
        // confirmImg: [
        //   { required: true, message: "修好照片不能为空", trigger: "blur" },
        // ],
      },
    };
  },
  created() {
    this.getMenus();
  },
  methods: {
    handleChange() {},
    getMenus() {
      this.$http({
        url: this.$http.adornUrl("/sys/dormarea/getCategory"),
        method: "get",
      }).then(({ data }) => {
        console.log("成功获取到菜单数据...", data.data);
        this.menus = data.data;
      });
    },

    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/dormrepair/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.dormId = data.dormRepair.dormId;
              this.dataForm.reason = data.dormRepair.reason;
              this.dataForm.img = data.dormRepair.img;
              this.dataForm.status = data.dormRepair.status;
              this.dataForm.fixId = data.dormRepair.fixId;
              this.dataForm.confirmImg = data.dormRepair.confirmImg;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/dormrepair/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              dormId: this.dormValue[2],
              reason: this.dataForm.reason,
              img: this.dataForm.img,
              // status: this.dataForm.status,
              // fixId: this.dataForm.fixId,
              // confirmImg: this.dataForm.confirmImg,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
